package com.CustomClasses;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.os.AsyncTask;
import android.util.Log;

public class FetchMySQL extends AsyncTask<String, String, List<HashMap<String, String>>>{


	private static final String TABLE_NAME = "tableName";

	// Creating JSON Parser object
	JSONParser jParser = new JSONParser();
	
	// url to get all entries list
	private static String url_all_products = "http://www.worldcupcompendium.com/android_connect/get_all_products.php";


	
	// JSON Node names
	protected static final String TAG_SUCCESS = "success";
	
	private static final String TAG_USER_ID = "user_id";
	
//	protected static HashMap<String, List<HashMap<String, String>>> userAnswers = new HashMap<String, List<HashMap<String,String>>>();;
	protected String tableName;
	protected List<String> columnsName;
	protected String userId;
	protected List<NameValuePair> params = new ArrayList<NameValuePair>();
	// entries JSONArray
	JSONArray entries = null;
	
	public FetchMySQL(String tableName, List<String> columnsName, String userId) {
		this.tableName = tableName;
		this.columnsName = columnsName;
		this.userId = userId;
		
		params.add(new BasicNameValuePair(TAG_USER_ID, userId));
		params.add(new BasicNameValuePair(TABLE_NAME, tableName));
		int cont = 0;
		StringBuilder sBuilder = new StringBuilder();
		for(String column : columnsName){

			sBuilder.append(column);
			cont++;
			if(cont<columnsName.size()){
			sBuilder.append(";");
			}
		}
		params.add(new BasicNameValuePair("columns", sBuilder.toString()));

	}
	

		


		/**
		 * getting All entries from url
		 * */
		protected List<HashMap<String, String>> doInBackground(String... args) {
			// Building Parameters
			
	
			ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String,String>>();

			// getting JSON string from URL
			
			JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
			
			// Check your log cat for JSON reponse
			Log.d("All Products: ", json.toString());

		
			
			try {
				// Checking for SUCCESS TAG
				int success = json.getInt(TAG_SUCCESS);

				if (success == 1) {
					// entries found
					// Getting Array of Products
					entries = json.getJSONArray(tableName);

					// looping through All Products
					for (int i = 0; i < entries.length(); i++) {
						JSONObject c = entries.getJSONObject(i);
						
						HashMap<String, String> map = new HashMap<String, String>();
						
						for(String column:columnsName){
							map.put(column, c.getString(column));
						}					
						
						// adding HashList to ArrayList
						arrayList.add(map);
					}
				} 
			} catch (JSONException e) {
				e.printStackTrace();
			}
	 
			
			return arrayList;
		}
}
